
const service = require('./getFlowersService.js')
const utilService = require('../../../utils/utilService.js');
const httpUtils = require('../../../utils/httpUtils.js');

var shareTicket
Page({

  /**
   * 页面的初始数据
   */
  data: {
    favStorys: null,
    items: [],
    Hidden: false,
    viewHidden: true,
    types: 1,
    shareData:null,
    homeHidden: false,
    shareMsg: {},
    goInvitate: false,
  },
  
  onLoad: function (options) {
    
    var that = this
    this.getShareMessage(2)
    let types = options.type?options.type:this.data.types;
    if (types == 1){
      that.setData({ homeHidden: true })
    }else{
      that.setData({ homeHidden: false })
    }
    that.setDataSource()
    wx.showShareMenu({
      withShareTicket: true //要求小程序返回分享目标信息
    })    
  },

  onReady: function(){
    this.dialog = this.selectComponent("#maskView");
  },

  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data
        })
      },
      function (error) {

      }
    );
  },
  setDataSource: function() {
    var that = this
    var parma = {
      userId: getApp().globalData.userId
    }
    service.requestTest(parma, function (data) {
      var array = data.tUserTaskEntitys
      getApp().globalData.userSweets = data.number
      that.setData({ favStorys: data, items: array });
    });
  },
  /**
   * 点击事件
   */
  fansBtn: function (e) {
    var that = this;
    this.setData({
      goInvitate: false
    })
    console.log(e.currentTarget.dataset.id)
    if (e.currentTarget.dataset.id.code == 1){
      //分享微信群
      // this.goReceoveBtn()
    } else if (e.currentTarget.dataset.id.code == 21){
      //关注公众号
      this.goTask()
    } else if (e.currentTarget.dataset.id.code == 2) {
      //发布宝贝
      this.goRelease()
    } else if (e.currentTarget.dataset.id.code == 3) {
      //首次送出宝贝
      this.goGiving()
    } else if (e.currentTarget.dataset.id.code == 4) {
      //发布感谢视频
      this.goThanks()
    } else if (e.currentTarget.dataset.id.code == 5) {
      //邀请新用户
      // this.goInvitate()
      this.setData({
        goInvitate: true
      })
    } 
    // else if (e.currentTarget.dataset.id.code == 7) {
    //   //领取新人红包
    //   this.receiveRedEnvelope()
    // }
     else if (e.currentTarget.dataset.id.code == 7) {
      //领取新人免邮卷
      this.receiveSecurities()
    } else if (e.currentTarget.dataset.id.code == 10) {
      //签到
      wx.navigateTo({
        url: '/redPacket/pages/signIn/signIn'
      })
    }
  },
  /**
   * 果冻记录
   */
  flowerDetail: function() {
    wx.navigateTo({
      url: '/pages/awardDetails/awardDetails',
    })
  },
  /**
   * 分享微信群 去领取
   */
  goReceoveBtn: function () {
    // wx.showShareMenu({
    //   withShareTicket: true
    // })
    // wx.navigateTo({
    //   url: '../../../pages/signIn/signIn',
    // })
  },
  /**
     * 做任务
     */
  goTask: function () {
    // this.showTost('需要开通服务号');
    wx.navigateTo({
      url: '../../../pages/goodsShare/goodsShare?pageType=' + "2",
    })
  },
  /**
  * 提交formid
  */
  submitForm: function (e) {
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 20);
  },
  /**
     * 完善资料  去完成
     */
  goComplete: function () {
    // this.showTost('点击了去完成');
    wx.navigateTo({
      url: '../../../pages/addProduct/myAddress/myAddres',
    })
  },
  /**
     * 发布宝贝  去发布
     */
  goRelease: function () {
    // this.showTost('点击了去发布');
    wx.switchTab({
      url: '../../../pages/addProduct/shareproducts/shareProduct',
    })
  },
  /**
     * 首次送出宝贝  去发布
     */
  goGiving: function () {
    // this.showTost('点击了去发布');
    wx.switchTab({
      url: '../../../pages/addProduct/shareproducts/shareProduct'
    })
  },
  /**
     * 发布感谢视频  去发布
     */
  goThanks: function () {
    // this.showTost('点击了去发布');
    wx.navigateTo({
      url: './uploadVideo/uploadVideo'
    })
  },
  /**
     * 邀请新用户  去邀请
     */
  goInvitate: function () {
    let videoUrl = getApp().globalData.baseUrl + '/resources/ad/happy-jelly.mp4';
    wx.navigateTo({
      url: '/pages/goodsShare/goodsShare?pageType=3&videoUrl=' + encodeURIComponent(videoUrl),
    })
  },
  /**
     * 领取新人红包  去领取
     */
  receiveRedEnvelope: function () {
    // this.showTost('点击了去领取');
    wx.navigateTo({
      url: '/goods/pages/goodsDetails/goodsDetails?goods_type=red_package'
    })
  },
  /**
     * 领取新人免邮卷  去领取
     */
  receiveSecurities: function () {
    // this.showTost('点击了去领取');
    wx.navigateTo({
      url: '/goods/pages/goodsDetails/goodsDetails?goods_type=red_package'
    })
  },
  
  showTost: function (str) {
    wx.showToast({
      title: str,
      icon: 'succes',
      duration: 1000,
      mask: true
    })
  },
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg.title;
    var imageUrl = this.data.shareMsg.pic;
    var path = "/pages/home/home?userId=" + getApp().globalData.userId
    var that = this

    return {
      title: title,
      path: path,
      imageUrl: imageUrl,

      success: function (r) {
        if (that.data.goInvitate){
            wx.showToast({
              title: '分享成功',
              icon: "none",
            })
            return;
        }
        if (res.target.id == 2){
          //获取用户设备信息  
          wx.getSystemInfo({
            success: function (d) {
              console.log(d);
              //判断用户手机是IOS还是Android
              if (d.platform == 'android') {
                wx.getShareInfo({//获取群详细信息  
                  shareTicket: r.shareTickets[0],
                  success: function (res) {
                    var parma = {
                      userId: getApp().globalData.userId,
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                    }
                    service.requestShareWx(parma, function (data) {
                      if (data.code != 200) {
                        wx.showToast({
                          title: data.errMsg,
                          icon: "none"
                        })
                        return
                      }
                      if (data.isViolent) {//
                        //暴击效果
                         var bingo = that.selectComponent("#bingo");
                         bingo.show(data.multiple, data.count);
                      } else {
                        console.log(data)
                        that.dialog.show();
                        that.setData({ shareData: data, viewHidden: false });
                      }
                    });
                    //这里写你分享到群之后要做的事情  
                  },
                  fail: function (r) {//这个方法就是分享到的是好友，给一个提示  
                    wx.showModal({
                      title: '提示',
                      content: '分享好友无效，请分享群',
                      success: function (r) {
                        if (res.confirm) {
                          console.log('用户点击确定')
                        } else if (res.cancel) {
                          console.log('用户点击取消')
                        }
                      }
                    })
                  }
                })
              }
              if (d.platform == 'ios') {//如果用户的设备是IOS  
                if (r.shareTickets != undefined) {
                  console.log("分享的是群");
                  shareTicket = r.shareTickets[0];
                  that.setShareData();
              
                } else {//分享到个人要做的事情，我给的是一个提示  
                  console.log("分享的是个人");
                  wx.showModal({
                    title: '提示',
                    content: '分享好友无效，请分享群',
                    success: function (r) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                }
              }

            },
            fail: function (r) {
            }
          })
        }else{
          console.log("*****" + r.shareTickets[0]);
          shareTicket = r.shareTickets[0]
          that.setShareData()
        }
      },
      fail: function (res) {
      }
    }
  },
  //分享请求
  setShareData: function() {
    var that = this
    let OBJECT = {
      shareTicket : shareTicket,
      timeout:1000,
      success	:function(res){
        console.log("*****" + JSON.stringify(res));
        var parma = {
          userId: getApp().globalData.userId,
          encryptedData: res.encryptedData,
          iv: res.iv,
        }
        service.requestShareWx(parma, function (data) {
          if (data.code != 200)
          {
            wx.showToast({
              title: data.errMsg,
              icon: "none"
            })
            return
          }
          if (data.isViolent){//
            //暴击效果
              var bingo = that.selectComponent("#bingo");
              bingo.show(data.multiple, data.count);
          }else{
            console.log(data)
            that.dialog.show();
            that.setData({ shareData: data, viewHidden: false });
          }
        });
      },
    }
    wx.getShareInfo(OBJECT)
    
  },
  //收下啦
  receiveClick: function(){
    this.setDataSource()
  },
  //返回首页
  homeBtnClick: function () {
    wx.switchTab({
      url: '/pages/home/home'
    })
    that.setData({ homeHidden: true })
  }
  
})